package com.unionpay.upmp.sdk.service;

import java.util.Map;

import org.apache.log4j.Logger;

import com.unionpay.upmp.sdk.conf.UpmpConfig;
import com.unionpay.upmp.sdk.util.HttpUtil;
import com.unionpay.upmp.sdk.util.UpmpCore;


/**
 * 中国银联手机支付相关代码
 * */
public class UpmpService{
	static final Logger logger = Logger.getLogger(UpmpService.class);

	/**
	 * 交易接口处理
	 * @param req 请求要素
	 * @param resp 应答要素
	 * @return 是否成功
	 */
	public static boolean trade(Map<String, String> req, Map<String, String> resp) {
		String nvp = buildReq(req);
		String respString = HttpUtil.post(UpmpConfig.PHONE_TRADE_URL, nvp);
		return verifyResponse(respString, resp);
	}
	
	/**
	 * 交易查询处理
	 * @param req 请求要素
	 * @param resp 应答要素
	 * @return 是否成功
	 */
	public static boolean query(Map<String, String> req, Map<String, String> resp){
		String nvp = buildReq(req);
		String respString = HttpUtil.post(UpmpConfig.PHONE_QUERY_URL, nvp);
		return verifyResponse(respString, resp);
	}
	
    /**
     * 拼接保留域
     * @param req 请求要素
     * @return 保留域
     */
    public static String buildReserved(Map<String, String> req) {
        StringBuilder merReserved = new StringBuilder();
        merReserved.append("{");
        merReserved.append(UpmpCore.createLinkString(req, false, true));
        merReserved.append("}");
        return merReserved.toString();
    }
	
	/**
	 * 拼接请求字符串
	 * @param req 请求要素
	 * @return 请求字符串
	 */
	public static String buildReq(Map<String, String> req) {
	    // 除去数组中的空值和签名参数
        Map<String, String> filteredReq = UpmpCore.paraFilter(req);
		// 生成签名结果
		String signature = UpmpCore.buildSignature(filteredReq);

		// 签名结果与签名方式加入请求提交参数组中
		filteredReq.put(UpmpConfig.SIGNATURE, signature);
		filteredReq.put(UpmpConfig.SIGN_METHOD, UpmpConfig.SIGN_TYPE);

		return UpmpCore.createLinkString(filteredReq, false, true);
	}
	
    /**
     * 异步通知消息验证
     * @param para 异步通知消息
     * @return 验证结果
     */
    public static boolean verifySignature(Map<String, String> para) {
        String respSignature = para.get(UpmpConfig.SIGNATURE);
        // 除去数组中的空值和签名参数
        Map<String, String> filteredReq = UpmpCore.paraFilter(para);
        String signature = UpmpCore.buildSignature(filteredReq);
        if (null != respSignature && respSignature.equals(signature)) {
			return true;
		}else {
            return false;
        }
    }
	
	/**
	 * 应答解析
	 * @param respString 应答报文
	 * @param resp 应答要素
	 * @return 应答是否成功
	 */
	private static boolean verifyResponse(String respString, Map<String, String> resp) {
		if (respString != null && !"".equals(respString)) {
			// 请求要素
			Map<String, String> para;
			try {
				para = UpmpCore.parseQString(respString);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				return false;
			}
			boolean signIsValid = verifySignature(para);

			resp.putAll(para);
			
            if (signIsValid) { 
                return true;
            }else {
                return false;
            }
				
		}
		return false;
	}
}
	